package com.controller;

import com.model.operation.UserOperate;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by LPFma on 2016/12/5 0005.
 */
@WebServlet(name = "AddCoin", urlPatterns = "/AddCoin")
public class AddCoin extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /*init response*/
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        response.addHeader("Access-Control-Allow-Origin","*");
        response.addHeader("Access-Control-Allow-Credentials", "true");
        response.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");

        /*post arguments*/
        String moviename;
        String username;
        String status = "unLogin";

        do {
            /* get username from session */
            username = (String) request.getSession(true).getAttribute("username");
            if (username == null) break;

            moviename = request.getParameter("moviename");

            UserOperate userOperate = new UserOperate();
            status = userOperate.AddCoin(username, moviename);
        } while (false);

        String finalStatus = status;
        Map map = new HashMap<String, String>() {
            {
                put("status", finalStatus);
            }
        };

        /* send json */
        JSONObject jsonObject = JSONObject.fromObject(map);
        response.getWriter().write(jsonObject.toString());
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
